####CONDITIONAL MARGINAL EFFECT OF UNAUTHORIZED PROTEST TREATMENT####
coefficients <- read.csv("Code/Book5.csv") ##Read CME estimated with Delta method in Stata
theme_set(theme_minimal() +
            theme(panel.border = element_rect(fill = NA)))

hues <- seq(15, 375, length = 7 + 1)
cols <-  hcl(h = hues, l = 65, c = 100)[c(1, 2)]

names(cols) <- c("Protester approval", "Protest support")
shapes <- c(15, 18)
names(shapes) <- c("Protester approval", "Protest support")

# make figures
coefficients$QOI <- factor(coefficients$QOI, levels=c("Strongly disagree", "Somewhat disagree", "Neither agree nor disagree", "Somewhat agree", "Strongly agree"))
coefficients$Outcome <- factor(coefficients$Outcome, levels=c("Protester approval", "Protest support"))

first_margins<-ggplot(coefficients[is.element(coefficients$Outcome,c("Protester approval",
                                                      "Protest support")),], aes(x = QOI, y = Unauthorized, group = Outcome,
                                                                       colour = Outcome, pch = Outcome)) +
  geom_hline(yintercept = 0, linetype = 2, size = 1, colour = "black") +
  geom_point(size = 5, position = position_dodge(width = 0.5)) +
  geom_errorbar(size = 0.5, aes(ymin = lower_ci,
                                ymax = upper_ci),
                position = position_dodge(width = 0.5), width = 0) +
  xlab("") +
    ylab("Unauthorized Event") +
  labs(color = "Outcome", shape = "Outcome") +
  scale_color_manual(values = cols[c("Protester approval","Protest support")]) +
  scale_shape_manual(values = shapes[c("Protester approval","Protest support")]) +
  theme(axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 0.5)) 


first_margins
fm<-first_margins + theme(legend.position = "none") + 
  theme(axis.text.x = element_text(size = 10))  +
  theme(axis.text.y = element_text(size = 14))  +
  scale_color_grey(start=0.5, end=0.3) 
fm

####CONDITIONAL MARGINAL EFFECT OF VIOLENT PROTESTERS TREATMENT####
coefficients <- read.csv("Code/Book6.csv") ##Read CME estimated with Delta method in Stata
theme_set(theme_minimal() +
            theme(panel.border = element_rect(fill = NA)))

hues <- seq(15, 375, length = 7 + 1)
cols <-  hcl(h = hues, l = 65, c = 100)[c(1, 2)]

names(cols) <- c("Protester approval", "Protest support")
shapes <- c(15, 18)
names(shapes) <- c("Protester approval", "Protest support")

coefficients$QOI <- factor(coefficients$QOI, levels=c("Strongly disagree", "Somewhat disagree", "Neither agree nor disagree", "Somewhat agree", "Strongly agree"))
coefficients$Outcome <- factor(coefficients$Outcome, levels=c("Protester approval", "Protest support"))

second_margins<-ggplot(coefficients[is.element(coefficients$Outcome,c("Protester approval",
                                                                     "Protest support")),], aes(x = QOI, y = Violent, group = Outcome,
                                                                                                colour = Outcome, pch = Outcome)) +
  geom_hline(yintercept = 0, linetype = 2, size = 1, colour = "black") +
  geom_point(size = 5, position = position_dodge(width = 0.5)) +
  geom_errorbar(size = 0.5, aes(ymin = lower_ci,
                                ymax = upper_ci),
                position = position_dodge(width = 0.5), width = 0) +
  xlab("") +
  ylab("Protesters Violent") +
  labs(color = "Outcome", shape = "Outcome") +
  scale_color_manual(values = cols[c("Protester approval","Protest support")]) +
  scale_shape_manual(values = shapes[c("Protester approval","Protest support")]) +
  theme(axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 0.5)) 

second_margins
sm<-second_margins + theme(legend.position = "none") + 
  theme(axis.text.x = element_text(size = 10))  +
  theme(axis.text.y = element_text(size = 14))  +
  scale_color_grey(start=0.5, end=0.3) 
sm

####CONDITIONAL MARGINAL EFFECT OF ARRESTS TREATMENT####
coefficients <- read.csv("Code/Book7.csv") ##Read CME estimated with Delta method in Stata
theme_set(theme_minimal() +
            theme(panel.border = element_rect(fill = NA)))

hues <- seq(15, 375, length = 7 + 1)
cols <-  hcl(h = hues, l = 65, c = 100)[c(1, 2)]

names(cols) <- c("Protester approval", "Protest support")
shapes <- c(15, 18)
names(shapes) <- c("Protester approval", "Protest support")

coefficients$QOI <- factor(coefficients$QOI, levels=c("Strongly disagree", "Somewhat disagree", "Neither agree nor disagree", "Somewhat agree", "Strongly agree"))
coefficients$Outcome <- factor(coefficients$Outcome, levels=c("Protester approval", "Protest support"))

third_margins<-ggplot(coefficients[is.element(coefficients$Outcome,c("Protester approval",
                                                                      "Protest support")),], aes(x = QOI, y = Arrests, group = Outcome,
                                                                                                 colour = Outcome, pch = Outcome)) +
  geom_hline(yintercept = 0, linetype = 2, size = 1, colour = "black") +
  geom_point(size = 5, position = position_dodge(width = 0.5)) +
  geom_errorbar(size = 0.5, aes(ymin = lower_ci,
                                ymax = upper_ci),
                position = position_dodge(width = 0.5), width = 0) +
  xlab("") +
  ylab("Protesters Arrested") +
  labs(color = "Outcome", shape = "Outcome") +
  scale_color_manual(values = cols[c("Protester approval","Protest support")]) +
  scale_shape_manual(values = shapes[c("Protester approval","Protest support")]) +
  theme(axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 0.5)) 

third_margins
tm<-third_margins + theme(legend.position = "none") + 
  theme(axis.text.x = element_text(size = 10))  +
  theme(axis.text.y = element_text(size = 14))  +
  scale_color_grey(start=0.5, end=0.3) 
tm

new<-ggarrange(fm, sm, tm, ncol=1, nrow=3, common.legend = TRUE, legend="bottom")
new

ggsave(height=8,
       width=11,
       "Figures/Figure_2.pdf",
       new)

